package com.work.straighttalk.ui;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;


import com.work.straighttalk.R;
import com.work.straighttalk.app.BaseActivity;
import com.work.straighttalk.adapter.MainViewPagerAdapter;

import com.work.straighttalk.ui.fragment.CallFragment;
import com.work.straighttalk.ui.fragment.ContactsFragment;
import com.work.straighttalk.ui.fragment.ConversationFragment;
import com.work.straighttalk.ui.i.MainVI;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * EMEventListener
 */
public class MainActivity extends BaseActivity implements MainVI{

    @Bind(R.id.mainToolbar)
    Toolbar mainToolbar;
    @Bind(R.id.mainTabLayout)
    TabLayout mainTabLayout;
    @Bind(R.id.mainViewPager)
    ViewPager mainViewPager;
    private List<Fragment> fragments;
    private MainViewPagerAdapter mainViewPagerAdapter;
    private String[] tabs = {"通话", "对话", "联系人"};
    private TextView checkTab;
    private ConversationFragment conversationFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        EMClient.getInstance().contactManager().setContactListener(new NewFriendsActivity.EMContactListeners());


    }


//        EMGroupManager.getInstance().loadAllGroups();

//        从本地数据库加载群组到内存的操作，如果你的应用中有群组，请加上这句话（要求在每次进入应用的时候调用）

//        EMChatManager.getInstance().loadAllConversations();




    @Override
    protected void initView() {
        super.initView();
        //
        mainToolbar.setTitle(getApplicationName());
        setSupportActionBar(mainToolbar);

        //conversationFragment
        conversationFragment = new ConversationFragment();
        fragments = new ArrayList<>();
        fragments.add(new CallFragment());
        fragments.add(conversationFragment);
        fragments.add(new ContactsFragment());
        mainViewPagerAdapter = new MainViewPagerAdapter(getSupportFragmentManager(), fragments);
        mainViewPager.setAdapter(mainViewPagerAdapter);
        mainTabLayout.setupWithViewPager(mainViewPager);
        for (int i = 0; i < fragments.size(); i++) {
            TabLayout.Tab tab = mainTabLayout.getTabAt(i);
            tab.setCustomView(getTabView(i));
        }

    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    /**
     * 用于获得tab布局
     *
     * @param position fragment的位置
     * @return tab的布局View
     */
    public View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_main, null);
        TextView tabText = (TextView) view.findViewById(R.id.tv_tab_main);
        checkTab = (TextView) view.findViewById(R.id.check_tab_main);
        tabText.setText(tabs[position]);
        return view;
    }


    /**
     * 用于获取应用程序名
     *
     * @return AppName
     */
    private String getApplicationName() {
        PackageManager pm = getPackageManager();
        ApplicationInfo ai = null;
        try {
            ai = pm.getApplicationInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String appName = (String) pm.getApplicationLabel(ai);
        return appName;
    }

    /**
     * @param count 未读消息数 对话数
     */
    @Override
    public void setCount(int count) {
        if (count != 0) {
            checkTab.setVisibility(View.VISIBLE);
            checkTab.setText(String.valueOf(count));
        } else {
            checkTab.setVisibility(View.GONE);
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
    }


    @Override
    protected void onPause() {
        super.onPause();
    }
}
